//
//  LGCancerPatientReportCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/7/22.
//

#import "LGCancerPatientReportCell.h"
#import "LGCheckPhotoTool.h"
@interface LGCancerPatientReportCell()
/**   */
@property (nonatomic , strong)UIView *iconView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *numberLabel;
/**   */
@property (nonatomic , strong)UIButton *openBtn;
/**   */
@property (nonatomic , strong)UIView *photoView;
/**   */
@property (nonatomic , strong)NSMutableArray *photoArray;
/**   */
@property (nonatomic , strong)NSArray *picArray;
/**   */
@property (nonatomic , assign)NSInteger count;
/**   */
@property (nonatomic , assign)CGFloat photoW;
/**   */
@property (nonatomic , assign)CGFloat photoH;

@end

@implementation LGCancerPatientReportCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.count = isPad?8:4;
        self.photoW = (Screen_W-viewPix(56)-viewPix(8)*(self.count-1))/self.count;
        [self createSubView];
    }
    return  self;
}
-(void)setTitleKey:(NSString *)titleKey{
    _titleKey = titleKey;
}

-(void)setOpened:(BOOL)opened{
    _opened = opened;
    self.openBtn.selected = opened;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.titleLabel.text = LGNSString(dataDic[self.titleKey]);
    self.numberLabel.text = [NSString stringWithFormat:@"共 %@ 张图片",dataDic[@"count"]];
    NSArray *itemArray = dataDic[@"items"];
    for (UIImageView *imageView in self.photoArray) {
        [imageView removeFromSuperview];
    }
    self.photoH = 0;
    [self.photoArray removeAllObjects];
    NSMutableArray *picArray = [NSMutableArray array];
    for (NSInteger i=0; i<itemArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(12)+(self.photoW+viewPix(8))*(i%self.count), viewPix(12)+(self.photoW+viewPix(10))*(i/self.count), self.photoW, self.photoW)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:itemArray[i][@"url"]]];
        imageView.userInteractionEnabled = YES;
        imageView.tag = 1000+i;
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(checkBigPhotoAction:)]];
        [self.photoView addSubview:imageView];
        [self.photoArray addObject:imageView];
        [picArray addObject:LGNSString(itemArray[i][@"url"])];
        self.photoH = kMaxY(imageView.frame)+viewPix(12);
    }
    self.picArray = picArray;
    self.openBtn.hidden = itemArray.count>self.count?NO:YES;
    CGFloat height = 0;
    if (itemArray.count>self.count && self.opened == YES) {
        height = self.photoH;
    }else if (itemArray.count>0 && self.opened == NO){
        height = self.photoW+viewPix(20);
    }else{
        height = 0;
    }
    [self.photoView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(height));
    }];
}

-(void)openButtonAction:(UIButton *)sender{
    sender.selected =! sender.selected;
    if (self.delegate && [self.delegate respondsToSelector:@selector(reportCellOpend:index:)]) {
        [self.delegate reportCellOpend:sender.selected index:self.index];
    }
}

-(void)checkBigPhotoAction:(UITapGestureRecognizer *)sender{
    UIImageView *imageView = (UIImageView *)sender.view;
    NSInteger index = imageView.tag-1000;
    LGCheckPhotoTool *bigPhotoView = [[LGCheckPhotoTool alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    bigPhotoView.title = @"患者报告";
    bigPhotoView.picArray = self.picArray;
    [bigPhotoView showViewAnimation:CGPointMake(0, 0) index:index];
    [[UIApplication sharedApplication].keyWindow addSubview:bigPhotoView];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.iconView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.numberLabel];
    [self.contentView addSubview:self.openBtn];
    [self.contentView addSubview:self.photoView];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.mas_right).offset(viewPix(8));
        make.centerY.equalTo(self.iconView);
    }];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right).offset(viewPix(12));
        make.centerY.equalTo(self.titleLabel);
    }];
    [self.openBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.centerY.equalTo(self.iconView);
        make.width.equalTo(@(viewPix(60)));
        make.height.equalTo(@(viewPix(30)));
    }];
    [self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(45));
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.bottom.equalTo(self.contentView).offset(-viewPix(10));
        make.height.equalTo(@(self.photoW+viewPix(24)));
    }];
}
-(UIView *)iconView{
    if(!_iconView){
        _iconView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(19), viewPix(20), 4, viewPix(14))];
        _iconView.backgroundColor = [UIColor colorWithString:@"#77D4C6"];
        _iconView.cornerRidus = 2;
    }
    return _iconView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(UILabel *)numberLabel{
    if(!_numberLabel){
        _numberLabel = [UILabel lableWithText:@"" colorString:@"#909399" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _numberLabel;
}

-(UIButton *)openBtn{
    if(!_openBtn){
        _openBtn = [UIButton buttonWithTitle:@"展开" titleFont:LGFont(14) textColor:@"#909399" imageName:@"grayTipDown"];
        _openBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _openBtn.bounds = CGRectMake(0, 0, viewPix(60), viewPix(30));
        [_openBtn setSelectTitle:@"收起" selectImage:@"grayTipUp"];
        [_openBtn addTarget:self action:@selector(openButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_openBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:5];
    }
    return _openBtn;
}

-(UIView *)photoView{
    if(!_photoView){
        _photoView = [[UIView alloc]init];
        _photoView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        _photoView.cornerRidus = 10;
        _photoView.clipsToBounds = YES;
    }
    return _photoView;
}

-(NSMutableArray *)photoArray{
    if(!_photoArray){
        _photoArray = [NSMutableArray array];
    }
    return _photoArray;
}

@end
